package com.quanyan.runningriding.web.running;


import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.runningriding.entity.vo.run.VideoVo;
import com.quanyan.runningriding.entity.vo.run.resp.VideoType;
import com.quanyan.runningriding.entity.vo.run.resp.VideoTypeDetail;
import com.quanyan.runningriding.service.RunVideoService;
import com.quanyan.runningriding.util.RunEnum;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * RunVideoController
 *
 * @author Tu Chun
 * @date 2016/8/23
 */
@RestController
@RequestMapping(value = "/api/run")
public class RunVideoController {
    @Autowired
    private RunVideoService runVideoService;
    private Logger logger = LoggerFactory.getLogger(RunVideoController.class);

    @RequestMapping(value = "/videoTypeList", method = RequestMethod.POST)
    public APIResponse<List<VideoType>> videoTypeList() {
        APIResponse<List<VideoType>> result = runVideoService.getVideoTypeList();
        return result;
    }

    @RequestMapping(value = "/videoTypeDetail", method = RequestMethod.POST)
    public APIResponse<PageObj<VideoTypeDetail>> videoTypeDetail(@RequestBody Map<String, Object> paramMap) {
        Byte type = MapUtils.getByte(paramMap, "type");
        if (type == null || type < 0) {
            return APIResponse.returnFail(RunEnum.PARAMETER_NOT_VALID);
        }
        Integer pageNum = (Integer) paramMap.get("pageNum");
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        Integer pageSize = (Integer) paramMap.get("pageSize");
        if (pageSize == null || pageSize <= 0) {
            pageSize = 20;
        }
        PageObj<VideoTypeDetail> result = runVideoService.getVideoTypeDetail(type, pageNum, pageSize);
        return APIResponse.returnSuccess(result);
    }

    /*@RequestMapping(value = "/userVideoRecord", method = RequestMethod.POST)
    public APIResponse<?> userVideoRecord(@RequestBody @Validated(Insert.class)VideoRecord videoRecord) {
        String userId = MapUtils.getString(paramMap, "userId");
        Integer videoId = MapUtils.getInteger(paramMap, "videoId");
        if (userId == null || videoId == null) {
            APIResponse.returnFail(RunEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        APIResponse<?> result = runVideoService.userVideoRecord(userId, videoId);
        return result;
    }*/

    @RequestMapping(value = "/videoRecommend", method = RequestMethod.POST)
    public APIResponse<List<VideoVo>> videoRecommend(@RequestBody Map<String, Object> paramMap) {
        String userId = MapUtils.getString(paramMap, "userId");
        Double distance = MapUtils.getDouble(paramMap, "distance");
        Long avgPace = MapUtils.getLong(paramMap, "avgPace");
        Long time = MapUtils.getLong(paramMap, "time");
        if (userId == null || distance == null || time == null || avgPace == null) {
            APIResponse.returnFail(RunEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        APIResponse<List<VideoVo>> result = runVideoService.videoRecommend(userId, distance, time, avgPace);
        return result;
    }


}
